package chapter3;

import java.text.DecimalFormat;

class Person {
	private String name;
	private int age;
	private boolean gender;
	public Person(String name, int age, boolean gender) {
		super();
		this.name = name;
		this.age = age;
		this.gender = gender;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (age != other.age)
			return false;
		if (gender != other.gender)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}

class Company {
	private String cname;

	public Company(String cname) {
		super();
		this.cname = cname;
	}

}

class Employee extends Person {
	private Company company;
	private double salary;

	public Employee(String name, int age, boolean gender, double salary,
			Company company) {
		super(name, age, gender);
		this.company = company;
		this.salary = salary;
	}

	public boolean equals(Object obj) {
		Employee e = (Employee) obj;
		DecimalFormat df = new DecimalFormat("#.##");
		String a = df.format(this.salary);
		String b = df.format(e.salary);
		if((this.company == null && e.company == null)&&super.equals(e) && a.equals(b)){
			return true;
		}
		else if (super.equals(e) && a.equals(b)
				&& ( this.company.equals(e.company))) {
			return true;
		} else {
			return false;
		}
	}
	
}

public class Test4_3 {
	public static void main(String[] args) {
	
		Company c1 = new Company("MicroSoft");
		Company c2 = new Company("IBM");
		Person e1 = new Employee("Li", 35, true, 60000.124, c1);
		Person e2 = new Employee("Li", 35, true, 60000.124, c1);
		Person e3 = new Employee("Li", 35, true, 60000.124, c2);
		Person e4 = new Employee("Li", 35, true, 60000.123, c2);
		Person e5 = new Employee("Li", 35, true, 60000.126, c2);
		Person e6 = new Employee("Li", 35, true, 60000.126, null);
		Person e7 = new Employee("Li", 35, true, 60000.136, null);
		System.out.println(e1.equals(e2));
		System.out.println(e1.equals(e3));
		System.out.println(e3.equals(e4));
		System.out.println(e3.equals(e5));
		System.out.println(e5.equals(e6));
		System.out.println(e6.equals(e7));
	}

}
